package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.CourseDao;
import vo.Course;

/**
 * Servlet implementation class OP_ModifCourseServlet
 */
public class OP_ModifCourseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OP_ModifCourseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Course course = new Course();
		CourseDao coursedao = new CourseDao();
		String flag = (String) request.getParameter("flag");
		String courseno = (String) request.getParameter("course_no");
		
		if(flag.compareTo("3") == 0) {//删除
			try {
				coursedao.deleteCourse(courseno);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.getRequestDispatcher("/jsp/Success.jsp").forward(request, response);
			return;
		}
		String coursename = (String) request.getParameter("course_name");
		String credit = (String) request.getParameter("course_credit");
		float creditFlo = Float.parseFloat(credit);
		String teano = (String) request.getParameter("tea_no");
		
		course.setCourseno(courseno);
		course.setCoursename(coursename);
		course.setCredit(creditFlo);
		course.setTeano(teano);
		
		if(flag.compareTo("1") == 0) {
			try {
				coursedao.insertCourse(course);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			try {
				coursedao.updateCourse(course);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		request.getRequestDispatcher("/jsp/Success.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
